package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserSign;

import java.sql.*;

public class UserSignDaoImpl extends TempDataObjectDao<UserSign> {

    public UserSign getUserSign(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_sign where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSign signData = parse(rs);
                return signData;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserSign data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_sign(userId,signTime,signDays,totalSignDays,onlineTime,onlineTimeMark) VALUES(?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getSignTime());
            call.setInt(i++, data.getSignDays());
            call.setInt(i++, data.getTotalSignDays());
            call.setLong(i++, data.getOnlineTime());
            call.setLong(i++, data.getOnlineTimeMark());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserSign data) {
        PreparedStatement call = null;
        String sql = "update t_u_sign set signTime=?,signDays=?,totalSignDays=?,onlineTime=?,onlineTimeMark=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getSignTime());
            call.setInt(i++, data.getSignDays());
            call.setInt(i++, data.getTotalSignDays());
            call.setLong(i++, data.getOnlineTime());
            call.setLong(i++, data.getOnlineTimeMark());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserSign parseData(ResultSet rs) throws SQLException {
        UserSign data = new UserSign();
        data.setUserId(rs.getLong("userId"));
        data.setSignTime(rs.getLong("signTime"));
        data.setSignDays(rs.getInt("signDays"));
        data.setTotalSignDays(rs.getInt("totalSignDays"));
        data.setOnlineTime(rs.getLong("onlineTime"));
        data.setOnlineTimeMark(rs.getLong("onlineTimeMark"));
        return data;
    }
}
